package com.oa.admin.controller;

import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.PurchaseRequestAddDTO;
import com.oa.admin.dto.PurchaseRequestListDTO;
import com.oa.admin.entity.PurchaseRequest;
import com.oa.admin.service.IPurchaseRequestService;
import com.oa.admin.vo.PurchaseRequestDetailsVO;
import com.oa.admin.vo.PurchaseRequestListVO;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/purchaseRequest")
@Api(description = "采购申请表模块")
public class PurchaseRequestController extends BaseController {

    private final IPurchaseRequestService purchaseRequestService;

    @Autowired
    public PurchaseRequestController(IPurchaseRequestService purchaseRequestService) {
        this.purchaseRequestService = purchaseRequestService;
    }

    /**
     * 根据Id查询
     *
     * @return Resp
     */
    @GetMapping("/list")
    @CommonController
    @Description("列表")
    @ApiOperation(value = "列表")
    public Resp<PurchaseRequest> list(PurchaseRequestListDTO purchaseRequestListDTO) {
        purchaseRequestListDTO.setUserId(getLoginUserId());
        return new Resp().success(purchaseRequestService.findPurchaseRequestList(purchaseRequestListDTO));
    }

    @GetMapping("/export")
    @CommonController
    @Description("列表")
    @ApiOperation(value = "列表")
    public void export(PurchaseRequestListDTO purchaseRequestListDTO) {
        purchaseRequestListDTO.setUserId(getLoginUserId());
        purchaseRequestService.export(purchaseRequestListDTO, response);
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<PurchaseRequestDetailsVO> findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(purchaseRequestService.findPurchaseRequestById(id));
    }


    /**
     * 新增
     *
     * @param purchaseRequestAddDTO PurchaseRequestAddDTO
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "采购申请对象") @RequestBody(required = false) PurchaseRequestAddDTO purchaseRequestAddDTO) throws BusinessException, DataIsEmptyException {
        purchaseRequestAddDTO.setProposer(getLoginUserId());
        purchaseRequestAddDTO.setId(null);
        return purchaseRequestService.add(purchaseRequestAddDTO);
    }

    /**
     * 根据Id修改
     *
     * @param purchaseRequest PurchaseRequest
     * @return Resp
     */
    @PutMapping("")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "采购申请表对象") @RequestBody(required = false) PurchaseRequest purchaseRequest) {
        if (purchaseRequestService.updateById(purchaseRequest)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        PurchaseRequest purchaseRequest = purchaseRequestService.selectById(id);
        if (purchaseRequest != null && purchaseRequest.getIsDeleted() == 2) {
            purchaseRequest.setIsDeleted(1);
            if (purchaseRequestService.updateById(purchaseRequest)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
